package by.grsu.as.page;


import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import by.grsu.as.dao.DaoAdmin;
import by.grsu.as.model.Admin;


public class EditAdminPage extends BasePage {
	@SpringBean
	private DaoAdmin adminDao;

	@SuppressWarnings("serial")
	public EditAdminPage(final Admin adminToEdit) {
		super();

		// create form fields and set data
		final TextField<String> nameField = new TextField<String>("name", new Model<String>(adminToEdit.getName()));
		final TextField<String> lastNameField = new TextField<String>("lastname", new Model<String>(adminToEdit.getLastname()));
		

		// create "form" component which is required for data retrieving from
		// client to server
		Form<Admin> form = new Form<Admin>("form", new Model<Admin>(adminToEdit)) {
			@Override
			public void onSubmit() {
				// retrieve data from the form and set it to the object
				adminToEdit.setName(nameField.getModelObject());
				adminToEdit.setLastname(lastNameField.getModelObject());
				// save updated object in DB
				adminDao.save(adminToEdit);
				// go to the list page
				setResponsePage(ListAdminsPage.class);
			}
		};

		// add form fields
		form.add(nameField);
		form.add(lastNameField);

		// add buttons
		form.add(new Button("saveLink"));
		form.add(new Link<Admin>("cancelLink") {
			private static final long serialVersionUID = -1521748836027504873L;

			@Override
			public void onClick() {
				setResponsePage(ListAdminsPage.class);
			}
		});
		// add form to the page
		add(form);

		// add special component which will allow to display "validation errors"
		// and feedback messages
		FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
		add(feedbackPanel);
	}

}
